<template>
  <header class="fixClass" :class="guiding ? 'no-position' : ''">
    <!-- <div class="head container flex flex-align-center flex-pack-justify"> -->
    <div class="head new-container flex flex-align-center flex-pack-justify">
      <div class="flex flex-align-center">
        <router-link class="a-sham" :to="{ path: '/' }">
          <img class="logo" :src="require('./images/logo_xcc.png')" alt="">
        </router-link>
        <div v-for="(item, index) in nav" :id="item.guide" :key="index" class="nav flex">
          <router-link class="a_sham" :to="item.navList[0].route">
            <div
              class="nav-label"
              :class="{ 'nav-active': headType === index }"
            >
              <span>
                {{ item.label }}
              </span>
              <i class="iconfont icon-arrowRight" />
            </div>
          </router-link>
          <div class="nav-child">
            <div class="navchild box-shadow flex flex-wrap">
              <router-link
                v-for="(it, idx) in item.navList"
                :key="idx"
                class="a_sham"
                :to="item.navList[idx].route"
              >
                <div
                  class="item"
                  :class="$route.name === it.route ? 'item-active' : ''"
                >
                  {{ it.label }}
                </div>
              </router-link>
            </div>
          </div>
        </div>
      </div>
      <div class="flex" style="align-items: flex-end;">
        <div
          v-if="!isLogin"
          class="login"
          @click="$router.push({ path: '/login' })"
        >
          注册&登录
        </div>
        <div
          v-show="
            isLogin &&
              (userInfo.account_type === 2 || userInfo.account_type === 4)
          "
          class="shopcart"
          @click="linkToShopCart"
        >
          <span class="shopcart-name">选品库</span>
          <i class="iconfont icon-gouwucheman" />
        </div>
        <div v-if="isLogin" id="guide4" class="user">
          <span @click="linkToBus">{{ userInfo.user_name }}</span>
          <i class="iconfont icon-arrowRight" />
          <el-avatar :size="38" :src="userInfo.avatar" />
          <div class="user-box">
            <UserCard :user-info="userInfo" />
          </div>
        </div>
      </div>
    </div>
  </header>
</template>

<script>
import UserCard from './components/UserCard'
export default {
  name: 'XccHeader',
  components: {
    UserCard
  },
  data() {
    return {
      nav: [
        {
          label: '主播报名',
          guide: 'guide1',
          navList: [
            // { label: '主播推荐', route: 'hjHomeBus' },
            { label: '主播报名', route: '/anchor' },
            // { label: '场次招商', route: 'hjMerchantsList' },
            { label: '主播活动', route: '/activity' },
            { label: '直播机构', route: '/mcn' }
            // {name:"带货视频",route:"hjVideoList"},
          ],
          searchList: [
            {
              label: '主播',
              route: '/anchor',
              placeholder: '搜索主播名称'
            },
            // {
            //   label: '场次',
            //   route: 'hjMerchantsList',
            //   placeholder: '搜索主播名称'
            // },
            {
              label: '活动',
              route: '/activity',
              placeholder: '搜索活动/主播名称'
            },
            {
              label: '直播机构',
              route: '/mcn',
              placeholder: '搜索机构名称/主播名称'
            }
          ]
        },
        {
          label: '选品库',
          guide: 'guide2',
          navList: [
            // { label: '商品推荐', route: 'hjHomeAnc' },
            { label: '供应链商品', route: '/supply' },
            { label: '商品中心', route: '/goods' },
            { label: '商家中心', route: '/business' },
            // { label: '直播基地', route: 'hjLiveBaseList' },
            { label: '专场活动', route: '/session' }
          ],
          searchList: [
            {
              label: '供应链商品',
              route: '/supply',
              placeholder: '搜索商品名称'
            },
            {
              label: '商品',
              route: '/goods',
              placeholder: '搜索商品名称'
            },
            {
              label: '商家',
              route: '/business',
              placeholder: '搜索商家名称'
            },
            // {
            //   label: '基地',
            //   route: 'hjLiveBaseList',
            //   placeholder: '搜索基地名称/店铺名称'
            // },
            {
              label: '活动',
              route: '/session',
              placeholder: '搜索专场活动/商家名称'
            }
          ]
        },
        {
          label: '数据查询',
          guide: 'guide3',
          navList: [
            { label: '数据首页', route: '/data' },
            { label: '主播数据', route: '/anchordata' },
            { label: '商品数据', route: '/goodsdata' },
            { label: 'MCN机构', route: '/mcndata' },
            // {label:"直播",route:"hjLiveData"},
            { label: '排行榜', route: '/rankdata' }
          ],
          searchList: [
            {
              label: '主播',
              route: 'hjAnchorDataList',
              placeholder: '请输入主播名称'
            },
            {
              label: '商品',
              route: 'hjGoodsDataList',
              placeholder: '请输入商品名称'
            },
            {
              label: '机构',
              route: 'hjOrganDataList',
              placeholder: '请输入机构、主播名称'
            }
          ]
        }
      ],
      searchIndex: 0, // 搜索活跃项
      keyword: '',
      isShowOption: false
    }
  },
  computed: {
    headType() {
      return this.$route.meta.headType
    },
    searchList() {
      if (this.headType === undefined) {
        return [
          {
            label: '主播',
            route: 'hjAnchorList',
            placeholder: '请输入主播名称'
          },
          {
            label: '商品',
            route: 'hjGoodsList',
            placeholder: '请输入商品名称'
          }
        ]
      } else {
        return this.nav[this.headType].searchList
      }
    },
    placeholder() {
      return this.searchList[this.searchIndex].placeholder
    },
    isLogin() {
      // 是否已登录
      return !!this.$store.getters.token
    },
    userInfo() {
      const userInfo = this.$store.getters.userInfo
      console.log(userInfo)
      return userInfo
    }
  },
  watch: {
    userInfo: {
      handler(v) {
        console.log(v)
        if (v.account_type === 2 || v.account_type === 4) {
          this.$store.dispatch('dict/cartShopNum')
        }
      }
    },
    $route: {
      handler(v) {
        this.keyword = v.query.keyword
        const actIndex = this.searchList.findIndex(it => it.route === v.name)
        if (actIndex !== -1) {
          this.searchIndex = actIndex
        } else {
          this.searchIndex = 0
        }
      }
    }
  },
  created() {
    if (this.isLogin) {
      this.$store.dispatch('user/getUserInfo')
    }
  },
  methods: {
    changeOption(index) {
      this.searchIndex = index
      this.isShowOption = false
      if (this.keyword) {
        this.toSearch()
      }
    },
    toSearch() {
      alert(123)
      // 查询
      const path = this.searchList[this.searchIndex].route
      this.$router.push({
        path,
        query: {
          keyword: this.keyword
        }
      })
    },
    linkToBus(str) {
      if (this.userInfo.account_type === 3) {
        // 商家用户是否认证权限一致，主播/机构需要认证才可进入后台
        this.$router.push({ name: 'busWelcome' })
      } else {
        // 获取实名认证状态
        this.$apianc.account.findIdentityVerification().then(({ data }) => {
          if (data.verificationStatus === 1) {
            this.$router.push({ name: 'ancWelcome' })
          } else {
            this.$confirm('实名认证后可以使用更多的业务功能', '立即实名认证', {
              confirmButtonText: '立即前往',
              showCancelButton: false,
              type: 'warning'
            }).then(() => {
              this.$router.push({ name: 'ancAccountAutonym' })
            })
            return
          }
        })
      }
    },
    linkToShopCart() {
      const url = this.$router.resolve({
        name: 'hjShopCartList'
      })
      window.open(url.href, '_blank')
    }
  }
}
</script>

<style lang="scss" scoped>
$theme-color: #ff0929;
.no-position{
  position: static!important;
  pointer-events: none;
}
.head {
  height: 70px;
  background: #ffffff;
}
.logo {
  width: 119px;
  height: 48px;
  margin-right: 50px;
  cursor: pointer;
}
.nav {
  position: relative;
  margin-right: 37px;
  &:last-of-type {
    margin-right: 0;
  }
  &:hover &-label {
    color: $theme-color;
    &::after {
      content: '';
      width: 28px;
      height: 2px;
      background: $theme-color;
      border-radius: 2px;
      position: absolute;
      bottom: 12px;
      left: calc(50% - 23px);
    }
    i {
      transform: rotate(-90deg);
    }
  }
  &:hover &-child {
    display: block;
  }
  &-label {
    height: 48px;
    line-height: 48px;
  }
  i {
    transition: transform 0.3s;
    display: inline-block;
    font-size: 12px;
    margin-left: 6px;
    transform: rotate(-270deg);
  }
  &-active {
    color: $theme-color;
    &::after {
      content: '';
      width: 28px;
      height: 2px;
      background: $theme-color;
      border-radius: 2px;
      position: absolute;
      bottom: 12px;
      left: calc(50% - 23px);
    }
  }
  &-child {
    display: none;
    position: absolute;
    top: 48px;
    left: calc(50% - 118px);
    z-index: 3000;
    padding-top: 23px;
    .navchild {
      background: #ffffff;
      padding: 9px 18px;
      border-radius: 3px;
      // height: 114px;
      width: 220px;
      .item {
        width: 92px;
        padding-left: 18px;
        line-height: 32px;
        cursor: pointer;
        &:hover {
          color: $theme-color;
        }
        &-active {
          color: $theme-color;
        }
      }
    }
  }
}
.search {
  font-size: 12px;
  &-option {
    color: $theme-text-su;
    cursor: pointer;
    position: absolute;
    top: 11px;
    left: 18px;
  }

  &-option-item {
    position: relative;
    left: -20px;
    top: 20px;
    width: 78px;
    text-align: center;
    background: #ffffff;
    box-shadow: 0px 0px 10px 0px rgba(0, 0, 0, 0.15);
    border-radius: 3px;
  }
  &-option-item li {
    height: 28px;
    line-height: 28px;
    font-size: 13px;
    color: #463f40;
  }
  // &-option-item li:last-child {
  //   padding-bottom: 14px;
  // }
  &-option-item li:hover {
    background: #f5f7fa;
  }
  .active-tab {
    color: #ff0929;
  }
  &-item {
    padding-left: 22px;
    span {
      display: inline-block;
      padding: 5px 6px;
      line-height: 1;
      cursor: pointer;
      margin-right: 4px;
    }
    &-active {
      color: #ffffff;
      background: $theme-color;
      border-radius: 4px 4px 0px 0px;
    }
  }
  &-input {
    height: 34px;
    display: inline-table;
    position: relative;
    input {
      display: table-cell;
      width: 394px;
      height: 34px;
      line-height: 20px;
      border-radius: 17px 0 0 17px;
      border: 2px solid $theme-color;
      border-right: 0;
      vertical-align: middle;
      outline: none;
      &::placeholder {
        color: #c6c6c6;
      }
    }
    .line {
      width: 1px;
      height: 30px;
      border-left: 1px solid #f4f4f4;
      position: absolute;
      top: 2px;
    }
    .btn {
      display: table-cell;
      width: 69px;
      height: 100%;
      background: $theme-color;
      border-radius: 0 17px 17px 0;
      text-align: center;
      vertical-align: middle;
      cursor: pointer;
      i {
        font-size: 20px;
        color: #ffffff;
        line-height: 34px;
      }
    }
  }
}
.login {
  width: 111px;
  height: 38px;
  font-weight: 500;
  background: $theme-color;
  border-radius: 24px;
  line-height: 38px;
  color: #ffffff;
  text-align: center;
  margin-left: 39px;
  cursor: pointer;
}
.user {
  cursor: pointer;
  height: 38px;
  margin-left: 16px;
  position: relative;
  > span {
    display: inline-block;
    max-width: 72px;
    font-size: 14px;
    color: #3f3f3f;
    vertical-align: middle;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  &:hover {
    .user-box {
      display: block;
    }
  }
  i {
    display: inline-block;
    font-size: 10px;
    margin-right: 10px;
    color: #000;
    margin-left: 8px;
    transform: rotate(-270deg);
  }
  &-box {
    display: none;
    position: absolute;
    top: 32px;
    left: -83px;
    width: 240px;
    z-index: 3000;
    padding-top: 10px;
  }
}
.shopcart {
  height: 34px;
  border-radius: 17px;
  text-align: center;
  line-height: 32px;
  margin-left: 8px;
  position: relative;
  cursor: pointer;
  &-name {
    margin: auto 10px;
    font-size: 16px;
    color: $theme-text;
  }
  i {
    font-size: 18px;
    margin-right: 3px;
    color: $theme-color;
    vertical-align: middle;
    position: relative;
    bottom: 2px;
  }
  .num {
    position: absolute;
    right: 0;
    top: -7px;
    color: #ffffff;
    background: $theme-color;
    font-size: 12px;
    height: 14px;
    line-height: 14px;
    min-width: 14px;
    padding: 0 2px;
    border-radius: 7px;
    text-align: center;
  }
}
.fixClass {
  position: fixed;
  background: #ffffff;
  z-index: 2002;
  left: 0;
  right: 0;
}
</style>
